// pages/cleanSelect/cleanSelect.js
const httprequest = require('../../utils/http_utils.js')
const dateutil = require('../../utils/date_util.js')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '历史任务',
    tasklist: [],
    historySearch: [], //历史任务
    search: '', //搜索文本框
    totalNum: 0, //任务总数
    PageSize: 10,
    CurrentPage: 1,
    TaskName: '', //保洁任务名称
    sc_height: '0',
  },

  //添加搜索历史
  insertSearch: function (e) {
    var arr_his = this.data.historySearch;
    console.log(arr_his)
    for (var i = 0; i < arr_his.length; i++) {
      if (arr_his[i] == e.detail.value) {
        arr_his.splice(i, 1)              //选中当前筛选项
      }
    }
    if (e.detail.value != '') {
      arr_his.push(e.detail.value)
    }
    if (arr_his.length <= 3) {
      this.setData({
        // sc_height: '200'
        sc_height: ((app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2) + 240
      })
    } else if (arr_his.length > 3) {
      this.setData({
        // sc_height: '250'
        sc_height: ((app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2) + 320
      })
    }

    if (arr_his.length > 6) {
      arr_his.splice(0, 1)            //去除数组开头
    }

    if (e.detail.value == '' || e.detail.value == null){
      this.setData({
        historySearch: arr_his,
        TaskName: '///'
      })
    }else{
      this.setData({
        historySearch: arr_his,
        TaskName: e.detail.value
      })
    }

    this.setData({
      PageSize: 10,
      CurrentPage: 1,
      tasklist: []
    })
    
    this.getHisTaskList();
    wx.setStorageSync('searchhistory', this.data.historySearch)
    console.log(wx.getStorageSync('searchhistory'))
  },

  //清除文本框
  resetInput() {
    this.setData({
      search: ''
    })
  },

  searchInput(e) {
    this.setData({
      search: e.detail.value
    })
  },

  //清除搜索历史
  clear() {
    this.setData({
      historySearch: [],
      // sc_height: '140'
      sc_height: ((app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2) + 160
    })
    wx.removeStorageSync('searchhistory')
  },

  //点击搜索历史
  searchhistory: function (e) {
    if (this.data.TaskName == e.currentTarget.dataset.taskname) {
      this.setData({
        TaskName: ''
      })
    } else {
      this.setData({
        PageSize: 10,
        CurrentPage: 1,
        tasklist: [],
        TaskName: e.currentTarget.dataset.taskname
      })
      this.getHisTaskList();
    }
  },

  //获取历史保洁任务
  getHisTaskList() {
    var that = this
    // that.setData({
    //   tasklist: []
    // })
    var param = {
      PageSize: that.data.PageSize,
      CurrentPage: that.data.CurrentPage,
      TaskName: that.data.TaskName, //保洁任务名称
    }
    console.log(JSON.stringify(param)+"-----------------------保洁筛选")
    httprequest.GetValidateRequest(httprequest.apiInfo.GetCleanTaskListMobileApi, param,
      function (res) {
        console.log('筛选当日未保洁任务', res)
        if (res.totalNum > 0) {
          let temparry = that.data.tasklist
          for (var i = 0; i < res.pageData.length; i++) {
            temparry.push(res.pageData[i])
          }
          that.setData({
            totalNum: res.totalNum,
            tasklist: temparry,
            showzanwu: false
          })
        } else {
          that.setData({
            showzanwu: true
          })
        }
      },
      function (err) {
        console.error(err)
        that.setData({
          showzanwu: true
        })
      }
    )
  },

  //上拉加载
  scrollToLower: function () {
    var that = this
    console.log(that.data.totalNum + "_______总条数")
    var pages = Math.round((that.data.totalNum / that.data.PageSize) * 100) / 100
    console.log(that.data.CurrentPage, pages)
    if (that.data.CurrentPage > pages) {
      that.setData({
        noMore: true
      })
      setTimeout(() => {
        that.setData({
          noMore: false
        })
      }, 2000)
    } else {
      that.setData({
        noMore: false,
        downloading: true
      })
      setTimeout(() => {
        that.setData({
          downloading: false
        })
      }, 3000)
      that.setData({
        CurrentPage: that.data.CurrentPage + 1
      })
      that.getHisTaskList();
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(wx.getStorageSync('searchhistory'))
    if (wx.getStorageSync('searchhistory') != undefined && wx.getStorageSync('searchhistory') != '') {
      this.setData({
        historySearch: wx.getStorageSync('searchhistory')
      })
      if (this.data.historySearch.length <= 3) {
        this.setData({
          // sc_height: '200'
          sc_height: ((app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2) + 240
        })
      } else if (this.data.historySearch.length > 3) {
        this.setData({
          // sc_height: '250'
          sc_height: ((app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2) + 320
        })
      }
    }
    console.log('---------导航栏高度--------' + this.data.sc_height)
    this.refreshView = this.selectComponent("#refreshView");
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    setTimeout(() => {
      console.log("scrollToUpper")
      var that = this
      if (that.data.TaskName == '' || that.data.TaskName == null){
        that.setData({
          TaskName: "///", //保洁任务名称
        })
      }
      that.setData({
        PageSize: 10,
        CurrentPage: 1,
        TaskName: that.data.TaskName, //保洁任务名称
        tasklist: []
      })
      that.getHisTaskList();
      this.refreshView.stopPullRefresh()
    }, 1000)
  },

  //触摸开始
  handletouchstart: function (event) {
    this.refreshView.handletouchstart(event)
  },
  //触摸移动
  handletouchmove: function (event) {
    this.refreshView.handletouchmove(event)
  },
  //触摸结束
  handletouchend: function (event) {
    this.refreshView.handletouchend(event)
  },
  //触摸取消
  handletouchcancel: function (event) {
    this.refreshView.handletouchcancel(event)
  },
  //页面滚动
  onPageScroll: function (event) {
    this.refreshView.onPageScroll(event)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})